﻿using PolePositionMobile.Entities;
using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Windows.Forms;
using System.Text;

namespace PolePositionMobile
{
    /// <summary>
    /// A form where the settings can be changed
    /// </summary>
    public partial class frmSettings : frmBase
    {
        //Created by: VMCoder
        //Creation date: -
        //Last updated by: VMCoder
        //Last update: 2008.04.10
        public frmSettings()
        {
            InitializeComponent();
        }
        private void frmSettings_Load(object sender, EventArgs e)
        {
            #region Config form
            lblTitle.Text = "PPI | Settings";
            lblLButtonText.Text = "OK";
            lblRButtonText.Text = "Cancel";
            #endregion
            lblError.Visible = false;
            LoadCombos();
            if (m_Settings.Timezone != Int32.MinValue)
                ddlTimezone.SelectedIndex = ddlTimezone.Items.IndexOf((m_Settings.Timezone >= 0 ? "+" : "") + m_Settings.Timezone.ToString());
        }
        private void LoadCombos()
        {
            for (Int32 _I = -12; _I <= 12; _I++)
                ddlTimezone.Items.Add((_I >= 0 ? "+" : "") + _I.ToString());
            ddlTimezone.SelectedIndex = 0;

            LoadVendorChildCombos(m_Settings.Vendor.Id);
            Utils.FillDropdown(ddlUtility, Logic.LogicLayer.GetUtilities(), typeof(EUtility), "Name", "Id", "");
            Utils.FillDropdown(ddlVendor, Logic.LogicLayer.GetVendors(), typeof(EVendor), "Name", "Id", "");

            ddlContact.SelectedIndex = ddlContact.Items.IndexOf(m_Settings.Contact);
            ddlProject.SelectedIndex = ddlProject.Items.IndexOf(m_Settings.Project);
            ddlUtility.SelectedIndex = ddlUtility.Items.IndexOf(m_Settings.Utility);
            ddlVendor.SelectedIndex = ddlVendor.Items.IndexOf(m_Settings.Vendor);

        }

        private void LoadVendorChildCombos(Int32 vendorId)
        {
            Utils.FillDropdown(ddlContact, Logic.LogicLayer.GetUsers(vendorId), typeof(EUser), "Name", "Id", "");
            Utils.FillDropdown(ddlProject, Logic.LogicLayer.GetProjects(vendorId), typeof(EProject), "Name", "Id", "");
        }
        protected override void lblLButton_Click(object sender, EventArgs e)
        {
            if (Validate())
                return;
            m_Settings.Timezone = ddlTimezone.SelectedIndex > -1 ? Int32.Parse(ddlTimezone.Items[ddlTimezone.SelectedIndex].ToString()) : Int32.MinValue;
            m_Settings.Contact = (EUser)ddlContact.SelectedItem;
            m_Settings.Project = (EProject)ddlProject.SelectedItem;
            m_Settings.Utility = (EUtility)ddlUtility.SelectedItem;
            m_Settings.Vendor = (EVendor)ddlVendor.SelectedItem;

            Logic.LogicLayer.SaveSettings(m_Settings);
            CloseForm();
        }

        private bool Validate()
        {
            bool _failed = false;
            StringBuilder _error = new StringBuilder();
            if (ddlProject.SelectedIndex == -1)
            {
                _error.AppendLine("No project was selected. Please select a project.");
                _failed = true;

            }
            if (ddlContact.SelectedIndex == -1)
            {
                _error.AppendLine("No user was selected. Please select a user.");
                _failed = true;
            }
            lblError.Text = _error.ToString();
            lblError.Refresh();
            lblError.Visible = _failed;
            return _failed;
        }
        protected override void lblRButton_Click(object sender, EventArgs e)
        {
            CloseForm();
        }
        private void CloseForm()
        {
            ShowLoading();
            Application.DoEvents();
            frmHomePage _Form = new frmHomePage();
            _Form.Settings = Settings;
            _Form.Show();
            this.Hide();
        }

        private void ddlVendor_SelectedIndexChanged(object sender, EventArgs e)
        {
            LoadVendorChildCombos(((EVendor)ddlVendor.SelectedItem).Id);
        }
    }
}